package cn.iocoder.yudao.module.search.api.search;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.search.api.search.dto.*;
import cn.iocoder.yudao.module.search.service.search.SearchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 搜索 API 实现类
 *
 * @author 芋道源码
 */
@RestController
@Validated
@Slf4j
@RequiredArgsConstructor
public class SearchApiImpl implements SearchApi {

    private final SearchService searchService;

    // ========== 索引管理 ==========

    @Override
    public CommonResult<Boolean> createIndex(SearchIndexReqDTO reqDTO) {
        try {
            Boolean result = searchService.createIndex(reqDTO);
            return success(result);
        } catch (Exception e) {
            log.error("[createIndex][创建索引失败] reqDTO: {}", reqDTO, e);
            return CommonResult.error(500, "创建索引失败: " + e.getMessage());
        }
    }

    @Override
    public CommonResult<Boolean> deleteIndex(Long tenantId, String indexName) {
        try {
            Boolean result = searchService.deleteIndex(tenantId, indexName);
            return success(result);
        } catch (Exception e) {
            log.error("[deleteIndex][删除索引失败] tenantId: {}, indexName: {}", tenantId, indexName, e);
            return CommonResult.error(500, "删除索引失败: " + e.getMessage());
        }
    }

    @Override
    public CommonResult<Boolean> existsIndex(Long tenantId, String indexName) {
        try {
            Boolean result = searchService.existsIndex(tenantId, indexName);
            return success(result);
        } catch (Exception e) {
            log.error("[existsIndex][检查索引存在性失败] tenantId: {}, indexName: {}", tenantId, indexName, e);
            return CommonResult.error(500, "检查索引存在性失败: " + e.getMessage());
        }
    }

    @Override
    public CommonResult<SearchIndexRespDTO> getIndex(Long tenantId, String indexName) {
        try {
            SearchIndexRespDTO result = searchService.getIndex(tenantId, indexName);
            return success(result);
        } catch (Exception e) {
            log.error("[getIndex][获取索引信息失败] tenantId: {}, indexName: {}", tenantId, indexName, e);
            return CommonResult.error(500, "获取索引信息失败: " + e.getMessage());
        }
    }

    @Override
    public CommonResult<List<SearchIndexRespDTO>> listIndexes(Long tenantId) {
        try {
            List<SearchIndexRespDTO> result = searchService.listIndexes(tenantId);
            return success(result);
        } catch (Exception e) {
            log.error("[listIndexes][获取索引列表失败] tenantId: {}", tenantId, e);
            return CommonResult.error(500, "获取索引列表失败: " + e.getMessage());
        }
    }

    // ========== 文档管理 ==========

    @Override
    public CommonResult<String> createDocument(SearchDocumentReqDTO reqDTO) {
        try {
            String result = searchService.createDocument(reqDTO);
            return success(result);
        } catch (Exception e) {
            log.error("[createDocument][创建文档失败] reqDTO: {}", reqDTO, e);
            return CommonResult.error(500, "创建文档失败: " + e.getMessage());
        }
    }

    @Override
    public CommonResult<Boolean> updateDocument(SearchDocumentReqDTO reqDTO) {
        try {
            Boolean result = searchService.updateDocument(reqDTO);
            return success(result);
        } catch (Exception e) {
            log.error("[updateDocument][更新文档失败] reqDTO: {}", reqDTO, e);
            return CommonResult.error(500, "更新文档失败: " + e.getMessage());
        }
    }

    @Override
    public CommonResult<Boolean> deleteDocument(Long tenantId, String indexName, String documentId) {
        try {
            Boolean result = searchService.deleteDocument(tenantId, indexName, documentId);
            return success(result);
        } catch (Exception e) {
            log.error("[deleteDocument][删除文档失败] tenantId: {}, indexName: {}, documentId: {}", tenantId, indexName, documentId, e);
            return CommonResult.error(500, "删除文档失败: " + e.getMessage());
        }
    }

    @Override
    public CommonResult<SearchDocumentRespDTO> getDocument(Long tenantId, String indexName, String documentId) {
        try {
            SearchDocumentRespDTO result = searchService.getDocument(tenantId, indexName, documentId);
            return success(result);
        } catch (Exception e) {
            log.error("[getDocument][获取文档失败] tenantId: {}, indexName: {}, documentId: {}", tenantId, indexName, documentId, e);
            return CommonResult.error(500, "获取文档失败: " + e.getMessage());
        }
    }

    @Override
    public CommonResult<SearchBulkRespDTO> bulkDocument(SearchBulkReqDTO reqDTO) {
        try {
            SearchBulkRespDTO result = searchService.bulkDocument(reqDTO);
            return success(result);
        } catch (Exception e) {
            log.error("[bulkDocument][批量操作文档失败] reqDTO: {}", reqDTO, e);
            return CommonResult.error(500, "批量操作文档失败: " + e.getMessage());
        }
    }

    // ========== 搜索查询 ==========

    @Override
    public CommonResult<SearchQueryRespDTO> search(SearchQueryReqDTO reqDTO) {
        try {
            SearchQueryRespDTO result = searchService.search(reqDTO);
            return success(result);
        } catch (Exception e) {
            log.error("[search][搜索查询失败] reqDTO: {}", reqDTO, e);
            return CommonResult.error(500, "搜索查询失败: " + e.getMessage());
        }
    }

}
